cmake_minimum_required(VERSION 3.15 FATAL_ERROR)
project(recipe-06-while VERSION 0.1 LANGUAGES CXX)

# case 1
set(counter 0)
while(counter LESS 10)
    message(STATUS "Counter is ${counter}")
    math(EXPR counter "${counter} + 1")
endwhile()
message("Loop finished. Counter value: ${counter}")

# case 2
set(my_list "item1;item2;item3")
list(LENGTH my_list list_length)
set(index 0)

while(index LESS list_length)
  list(GET my_list ${index} current_item)
  message("Item at index ${index}: ${current_item}")
  math(EXPR index "${index} + 1")
endwhile()
message("Loop list finished. index value: ${index}")

# case 3
set(file_content "line1\nline2\nEND_OF_DATA\nline4") # 模拟文件内容
string(REPLACE "\n" ";" file_lines "${file_content}") # 将换行符替换为分号，创建列表
list(LENGTH file_lines num_lines)
set(line_index 0)

while(line_index LESS num_lines)
  list(GET file_lines ${line_index} current_line)

  if(current_line STREQUAL "END_OF_DATA")
    message("Found END_OF_DATA. Exiting loop.")
    break() # 使用 break 命令退出循环
  endif()

  message("Line ${line_index}: ${current_line}")
  math(EXPR line_index "${line_index} + 1")
endwhile()
message("read file finished. line_index value: ${line_index}")

# case 4
set(counter 0)

while(TRUE) # 无限循环
  math(EXPR counter "${counter} + 1")

  if(counter GREATER 10)
    message("Breaking out of the loop.")
    break() # 退出循环
  endif()

  if(counter EQUAL 5)
    message("Skipping iteration with counter = 5.")
    math(EXPR counter "${counter} + 1")
    continue() # 跳过本次循环的剩余部分
  endif()

  message("Counter value: ${counter}")
endwhile()
